'''
# Coding:utf-8
# Project: iiop
# Author: rtf
# Time: 2021-07-14 14:37:30
# FileName: test.py
# Software: PyCharm
'''

from functools import reduce


def sum_dict(a, b):
    temp = dict()
    # python3,dict_keys类似set； | 并集
    for key in a.keys() | b.keys():
        try:
            temp[key] = sum([float(d.get(key, 0)) for d in (a, b)])
        except:
            temp[key] = a.get(key)
    return temp


def average(temp, dict_list_len):
    for k in temp.keys():
        try:
            if k in ["no", "key", "in_item_num"]:
                temp[k] = int(temp[k] // dict_list_len) + 1
            else:
                temp[k] = round(temp[k] / dict_list_len, 2)
        except:
            temp[k] = temp[k]
    return temp


def test():
    # [a,b,c]列表中的参数可以2个也可以多个，自己尝试。
    dict_list = [a, b, c]
    return print(average(reduce(sum_dict, dict_list), len(dict_list)))


a = {"total": 320250.04, "si_cost": 282616.5, "boe_cost": 3.9, "cdf_cost": 84.75, "sfd_cost": 37403.52, "sid_cost": 21.84, "sii_cost": 5.85, "dprd_cost": 0, "duddi_cost": 0, "other_cost": 82, "dsiccq_cost": 31.68, "si_workload": 1884110, "boe_workload": 26, "cdf_workload": 339, "sfd_workload": 178112, "sid_workload": 104, "sii_workload": 39, "dprd_workload": 0, "si_labor_rate": 0.15, "boe_labor_rate": 0.15, "cdf_labor_rate": 0.25, "duddi_workload": 0, "sfd_labor_rate": 0.21, "sid_labor_rate": "0.21", "sii_labor_rate": 5.85, "dprd_labor_rate": "", "dsiccq_workload": 176, "duddi_labor_rate": 0.18, "dsiccq_labor_rate": 0.18}
b = {"total": 612588.87, "si_cost": 538223.4, "boe_cost": 3.9, "cdf_cost": 107, "sfd_cost": 74113.2, "sid_cost": 21.84, "sii_cost": 5.85, "dprd_cost": 0, "duddi_cost": 31.68, "other_cost": 82, "dsiccq_cost": 0, "si_workload": 3588156, "boe_workload": 26, "cdf_workload": 428, "sfd_workload": 352920, "sid_workload": 104, "sii_workload": 39, "dprd_workload": 0, "si_labor_rate": 0.15, "boe_labor_rate": 0.15, "cdf_labor_rate": 0.25, "duddi_workload": 176, "sfd_labor_rate": 0.21, "sid_labor_rate": "0.21", "sii_labor_rate": 5.85, "dprd_labor_rate": "", "dsiccq_workload": 0, "duddi_labor_rate": 0.18, "dsiccq_labor_rate": 0.18}
c = {"total": 391100.67, "si_cost": 323060.4, "boe_cost": 3.9, "cdf_cost": 65, "sfd_cost": 67830, "sid_cost": 21.84, "sii_cost": 5.85, "dprd_cost": 0, "duddi_cost": 0, "other_cost": 82, "dsiccq_cost": 31.68, "si_workload": 2153736, "boe_workload": 26, "cdf_workload": 260, "sfd_workload": 323000, "sid_workload": 104, "sii_workload": 39, "dprd_workload": 0, "si_labor_rate": 0.15, "boe_labor_rate": 0.15, "cdf_labor_rate": 0.25, "duddi_workload": 0, "sfd_labor_rate": 0.21, "sid_labor_rate": "0.21", "sii_labor_rate": 5.85, "dprd_labor_rate": "", "dsiccq_workload": 176, "duddi_labor_rate": 0.18, "dsiccq_labor_rate": 0.18}

test()

items = [[{"a": 1, "b": 2}, {"a": 1, "b": 2}], [{"a": 2, "b": 4}, {"a": 1, "b": 2}]]
items = [[{"a": 1, "b": 2}], [{"a": 2, "b": 4}]]
temp = list()
for i, v in enumerate(items):
    child_temp = list()
    if len(v) == 1:
        child_temp = v[0]
    else:
        for _index, value in enumerate(v):
            child_temp.append(items[_index][i])
    temp.append(child_temp)

import numpy as np

in_items = list()
if np.array(temp).ndim == 1:
    in_items.append(average(reduce(sum_dict, temp), len(temp)))
else:
    for x in temp:
        in_items.append(average(reduce(sum_dict, x), len(temp)))

# print("======in_items=======", in_items)


items = [{"a": 1, "b": 2, "c": "item1"}, {"a": 1, "b": 2, "c": "item2"}, {"a": 1, "b": 2, "c": "item3"},
         {"a": 1, "b": 2, "c": "item1"}, {"a": 1, "b": 2, "c": "item2"}, {"a": 1, "b": 2, "c": "item3"}]

item = {"item1": [], "item2": [], "item3": []}
temp_item = dict()
for x in items:
    if x.get("c") in temp_item:
        temp_item.get(x.get("c")).append(x)
    else:
        temp_list = list()
        temp_list.append(x)
        temp_item[x.get("c")] = temp_list
print(temp_item)


for k, v in temp_item.items():
    print(k, v[0])
